
var event = {};
event.wsstatus = 0;
event.sn = 1;
event.paramset = 0;
var init = function (mac, roomid, userid){
      event.mac = mac;
      event.roomid = roomid;
      event.userid = userid;
      event.paramset = 1;
}

var connect = function(host){
  if(event.paramset == 0) return ;
  machineRun(0);
  event.ws = wx.connectSocket({
    url: host,
    success: function (res) {
      console.log("wss连接成功");
    },
    fail: function (err) {
      console.log("wss连接失败");
    }
  });

  event.open = wx.onSocketOpen(res => {

    // console.log(res);
    machineRun(1);
    register();
  });



  event.ws.onmessage = wx.onSocketMessage(e => {
    var data = JSON.parse(e.data);
    console.log(data);
    switch (data['msgtype']) {
      case 'ping':
        onping();
        break;
      case 'client_register':
        onregister(data);
        break;
      case 'client_connect':
        onconnect(data);
        break;
      case 'client_songlist':
      // console.log(1);
        onrecvsonglist(data);
        break;
      case 'client_sunglist':
        onrecvsunglist(data);
        break;
      case 'client_userlist':
        onrecvuserlist(data);
        break;
      case 'client_liveplay':
        onrecvliveplay(data);
        break;
      default: break;
    }
  })


}
function register() {

  var data = new Object();
  data.msgtype = "client_register";
  data.mac = event.mac;
  data.roomid = event.roomid;
  data.userid = event.userid;
  data.sn = event.sn++;
  // console.log(data);
  wx.sendSocketMessage({
    data: JSON.stringify(data)
  })
}

function onping(){
  var data = new Object();

  data.msgtype = "pong";
  // console.log(data);
  wx.sendSocketMessage({
    data: JSON.stringify(data)
  })
}

function onregister(data) {
  if (data['result'] == 0) {
    machineRun(2);
  }
  else {
    console.log("register err "+data['result']);
  }
}

function onconnect(data) {
  if (data['result'] == 0) {
    machineRun(2);
  }
  else {
    console.log("register err "+data['result']);
  }
}

function machineRun(status) {
  event.wsstatus = status;
  // try {
  //   if (event.showstatus
  //     && typeof (event.showstatus) == "function") {
      // event.showstatus(event.wsstatus);
    // }
  // } catch (e) { }
}

function onrecvsonglist(data) {
  // console.log(data);
  try {
    // console.log(event);
    // console.log(event.showsonglist);
    // if (event.showsonglist
    //   && typeof (event.showsonglist) == "function") {
        // console.log(data);
      event.showsonglist(data['songlist']);
    // }
  } catch (e) { }
}

function onrecvsunglist(data) {
  try {
  //   if (event.showsunglist
  //     && typeof (event.showsunglist) == "function") {
      event.showsunglist(data['sunglist']);
    // }
  } catch (e) { }
}

function onrecvuserlist(data) {
  try {
  //   if (event.showuserlist
  //     && typeof (event.showuserlist) == "function") {
      event.showuserlist(data['userlist']);
    // }
  } catch (e) { }
}

function onrecvliveplay(data) {
  try {
  //   if (event.showliveplay
  //     && typeof (event.showliveplay) == "function") {
      event.showliveplay(data);
    // }
  } catch (e) { }
}


var remotectl = function (action) {
  if (event.wsstatus <= 0) return;

  var data = new Object();
  var cmd = new Object();

  cmd.msgtype = "remotectl";
  cmd.action = action;
  cmd.sn = event.sn;

  data.msgtype = "client_remotectl";
  data.sn = event.sn++;
  data.cmd = cmd;
  wx.sendSocketMessage({
    data: JSON.stringify(data)
  })
};

var songselect = function (songid, istop) {
  if (event.wsstatus <= 0) return;

  var data = new Object();
  var cmd = new Object();

  cmd.msgtype = "songselect";
  cmd.songid = "" + songid;
  cmd.istop = istop;
  cmd.sn = event.sn;

  data.msgtype = "client_remotectl";
  data.sn = event.sn++;
  data.cmd = cmd;
  console.log(data);
  wx.sendSocketMessage({
    data: JSON.stringify(data)
  })
};

var songupper = function (songid, ID) {
  if (event.wsstatus <= 0) return;

  var data = new Object();
  var cmd = new Object();

  cmd.msgtype = "order_insert";
  cmd.songid = "" + songid;
  cmd.ID = ID;
  cmd.sn = event.sn;

  data.msgtype = "client_remotectl";
  data.sn = event.sn++;
  data.cmd = cmd;

  wx.sendSocketMessage({
    data: JSON.stringify(data)
  })
};

var songdel = function (songid, ID) {
  if (event.wsstatus <= 0) return;

  var data = new Object();
  var cmd = new Object();

  cmd.msgtype = "order_del";
  cmd.songid = "" + songid;
  cmd.ID = ID;
  cmd.sn = event.sn;

  data.msgtype = "client_remotectl";
  data.sn = event.sn++;
  data.cmd = cmd;

  wx.sendSocketMessage({
    data: JSON.stringify(data)
  })
};

var barrage = function (userid, imageurl, message, pic_url) {
  if (event.wsstatus <= 0) return;

  var data = new Object();
  var cmd = new Object();

  cmd.msgtype = "barrage";
  cmd.userid = "" + userid;
  cmd.message = "" + message;
  cmd.imageurl = "" + imageurl;
  cmd.pic_url = "" + pic_url;
  cmd.sn = event.sn;

  data.msgtype = "client_remotectl";
  data.sn = event.sn++;
  data.cmd = cmd;
  wx.sendSocketMessage({
    data: JSON.stringify(data)
  })
};

var uploadsong = function (songid, rec_id) {
  if (event.wsstatus <= 0) return;

  var data = new Object();
  var cmd = new Object();

  cmd.msgtype = "uploadsong";
  cmd.songid = "" + songid;
  cmd.rec_id = "" + rec_id;
  cmd.sn = event.sn;

  data.msgtype = "client_remotectl";
  data.sn = event.sn++;
  data.cmd = cmd;

  wx.sendSocketMessage({
    data: JSON.stringify(data)
  })
};


module.exports.init = init;
module.exports.connect = connect;
module.exports.remotectl = remotectl;
module.exports.songselect = songselect;
module.exports.songupper = songupper;
module.exports.songdel = songdel;
module.exports.barrage = barrage;
module.exports.uploadsong = uploadsong;
module.exports.event = event;
